package com.stations.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stations.common.web.domain.request.PageDomain;
import com.stations.common.web.domain.response.Result;
import com.stations.domain.ExceptionLog;
import com.stations.domain.TTransmitter;
import com.stations.domain.TTransmitterException;
import com.stations.domain.TTransmitterLog;
import com.stations.mapper.TTransmitterLogMapper;
import com.stations.mapper.TTransmitterMapper;
import com.stations.service.TransmitterSendService;
import com.stations.service.TrasmiterLogService;
import com.stations.service.TrasmiterService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


/**
 * Describe: 用户服务实现类
 * Author: Ljy
 * CreateTime: 2019/10/23
 */
@Service
public class TrasmiterLogServiceImpl implements TrasmiterLogService {

    @Resource(name = "TerminalRedisTemplate")
    RedisTemplate<String,Object> redisTemplate;

    @Resource
    private TrasmiterService sysTrasmiterService;

    @Resource
    TransmitterSendService transmitterSendService;

    @Resource
    private TTransmitterLogMapper tTransmitterLogMapper;
    /**
     * Describe: 获取发射机日志列表
     * Param ModelAndView
     * Return 获取发射机日志列表
     */
    @Override
    public List<ExceptionLog> getLogs() {
        List<TTransmitter> lisTTransmitter = sysTrasmiterService.getAllTransmitter();
        for (TTransmitter tTransmitter:lisTTransmitter) {
            //transmitterSendService.queryLog(tTransmitter,new Date(),new Date());
        }
        List<ExceptionLog> lisExceptionLog = (List)redisTemplate.opsForHash().values("TransmitterExceptionLog");
        return lisExceptionLog;
    }

    @Override
    public PageInfo<TTransmitterLog> selectTTransmitterLogPage(TTransmitterLog tTransmitterLog, PageDomain pageDomain) {

        TTransmitterLog[]  tTransmitterLognow;
        // 创建 SimpleDateFormat 实例
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if(tTransmitterLog.getStationCode()!=null){
            TTransmitter tTransmitter = transmitterSendService.getTTransmitter(tTransmitterLog.getStationCode(),tTransmitterLog.getTransCode());
            tTransmitterLognow = transmitterSendService.queryLog(tTransmitter,sdf.format(tTransmitterLog.getStartTime()),sdf.format(tTransmitterLog.getEndTime()));
            if(tTransmitterLognow.length>0){
                tTransmitterLogMapper.deleteByKey(tTransmitter.getStationCode(),tTransmitter.getTransCode());
            }
            for (int i = 0; i < tTransmitterLognow.length; i++) {
                tTransmitterLognow[i].setLogId(StrUtil.uuid());
                tTransmitterLognow[i].setTransCode(tTransmitter.getTransCode());
                tTransmitterLognow[i].setStationCode(tTransmitter.getStationCode());
                tTransmitterLognow[i].setSendTime(new Date());
                tTransmitterLogMapper.insert(tTransmitterLognow[i]);
            }
        }
        List<TTransmitterLog> data = tTransmitterLogMapper.selectTTransmitterLogList(tTransmitterLog);
        return new PageInfo<>(data);
    }

    public static void main(String args[]) throws JsonProcessingException {

        String xx = "{\"code\":\"0\",\"data\":{\"logList\":[{'logType':1},{'logType':22}]},\"msg\":\"\"}";
        Result result = JSON.parseObject(xx,Result.class);
        JSONObject jsonresult = JSON.parseObject(xx);
        JSONObject value = JSON.parseObject(jsonresult.getString("data"));
        ObjectMapper objectMapper = new ObjectMapper();
        TTransmitterLog[] tTransmitterLog = objectMapper.readValue(value.getString("logList"), TTransmitterLog[].class);
        System.out.println(tTransmitterLog[0]);
    }
}